/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iasd.youthshelter.datacore;

import java.util.concurrent.Semaphore;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Rui
 */
public class EntityManagerSemaphore {
    private static EntityManagerSemaphore instance;
    
    private Semaphore semaphore;
    private EntityManagerFactory entityManagerFactory;

    private EntityManagerSemaphore() {
        semaphore = new Semaphore(1);
        entityManagerFactory = Persistence.createEntityManagerFactory("YouthShelterH2");
    }
    
    public static EntityManagerSemaphore getInstance() {
        if(instance == null)
            instance = new EntityManagerSemaphore();
        return instance;
    }

    public Semaphore getSemaphore() {
        return semaphore;
    }

    public EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }
}
